﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Microsoft.Win32;

namespace HBAClient
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            

			string pathRegKey = "Software\\HBA";
			RegistryKey regKey = Registry.CurrentUser.OpenSubKey(pathRegKey);
			string codeValue = (string)regKey.GetValue("Code");

			if (codeValue != "SUCCESS")
			{
				MessageBox.Show("You don't have access to run this program");
			}
			else
			{
				Application.Run(new ClientMain());
			}
        }

		public static Int64 GetUserID()
		{
			int randomNumber;
			string pathRegKey = "Software\\HBA";
			RegistryKey regKey = Registry.CurrentUser.OpenSubKey(pathRegKey, true);
			string userIDValue = (string)regKey.GetValue("UserID");

			// if it's the first time that client run this program
			if (userIDValue == null)
			{
				Random rnd = new Random();
				randomNumber = rnd.Next(100000, 900000);
				regKey.SetValue("UserID", randomNumber.ToString(), RegistryValueKind.String);

				try
				{
					HBADBDataContext db = new HBADBDataContext();
					User newUser = new User();
					newUser.UserID = randomNumber;
					db.Users.InsertOnSubmit(newUser);
                    db.SubmitChanges();
					
				}
				catch (Exception ex)
				{
					return 0;
				}

				return Convert.ToInt64(randomNumber);
			}

			return Convert.ToInt64(userIDValue);
		}

    }
}
